<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/WEB-INF/webpage/common/taglibs.jspf"%>
<!DOCTYPE html>
<html>
<head>
    <title><spring:message code="sys.role.authMenu" /></title>
    <meta name="decorator" content="form"/> 
    <html:css name="jquery-ztree"/>
    <html:js  name="jquery-ztree"/>
    <script type="text/javascript" src="${staticPath}/vendors/jquery-ztree/zTree_v3/js/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="${staticPath}/vendors/jquery-ztree/zTree_v3/js/jquery.ztree.excheck.js"></script>
    <script type="text/javascript" src="${staticPath}/vendors/jquery-ztree/zTree_v3/js/jquery.ztree.exedit.js"></script>
    <style>
	    .ibox-content {
	   	  padding: 0px 0px 0px;
	   	}
	   	.float-box{
	   		height:25px;
	   		color:red;
	   		position:relative;
	   	}
	   	.float-box > ul{
	   		background-color:lightgray;
	   		position:absolute;
	   		top:24px;
	   		right:-150px;
	   		border:1px solid #eee;
	   		z-index:9999;
	   		display: none;
	   	}
	   	.float-box:hover > ul{
	   		display: block;
	   	}
	   	.menu{
	   		color: green;
	   	}
	   	.model{
	   		color: blue;
	   	}
	</style>
</head>

<body class="white-bg"  formid="modelMenuOrgForm">
    <form:form id="modelMenuOrgForm" modelAttribute="data" action="${adminPath}/workflow/actmenuorg/doSave" method="post" class="form-horizontal">
		<form:hidden nested="false" path="modelKey" defaultValue="${param.modelKey}"/>
		<form:hidden nested="false" path="menuId"/>
		<form:hidden nested="false" path="orgIds"/>
		<div>
			<div style="width: 45%;float: left">
				<ul id="menuTree" class="ztree"></ul>
			</div>
			<div style="width: 25%;float: left">
				<ul id="orgTree" class="ztree"></ul>
			</div>
		</div>
	</form:form>
	<script type="text/javascript">
	var menuTreeObj;
	var orgTreeObj;
	var menusNodes =${menus};//加载菜单
	var orgs = ${orgs};//加载组织
	var selects = ${selects};//加载已配置的数据
	var menuModels = ${menuModels};//组织已绑定的菜单-模型
	
	var selectMenu = null;//已选择的菜单
	var currMenuModels = new Array();

	/**
	 * @description 菜单树的ztree配置 .<br>
	 * @date 2018/3/19 .<br>
	 * @author zcg .<br>
	 */
	var menuTreeSetting = {
		callback: {
			onClick: menuOnClick
		},
		data: {
			key: {
				url: "notarget",
				icon: "notarget"
			},
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "parentId",
				rootPId: 0
			}
		}	
	}
	
	/**
	 * @description 组织树的ztree配置 .<br>
	 * @date 2018/3/19 .<br>
	 * @author zcg .<br>
	 */
	var orgTreeSetting = {
		check: {
			enable: true,
			chkboxType : {"Y": "s", "N": "s"}
		},
		data: {
			key: {
				url: "notarget",
				icon: "notarget"
			},
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "parentId",
				rootPId: 0
			}
		},
		view: {
			addDiyDom: addDiyDom
		}
	}
	 
	/**
	 * @description 添加需要绑定的菜单 .<br>
	 * @date 2019/1/14 .<br>
	 * @author zcg .<br>
	 */
	function addDiyDom(treeId, treeNode){
		 var nodeId = treeNode.id;
		 var aObj = $("#" +  treeNode.tId + '_a');
		 $.each(menuModels,function(index,item){
			 if(nodeId==index&&item.length>0){
				 var obj = JSON.stringify(item);
				 var editStr = "<a class='float-box'><span style='color: red;'>【已绑定"+item.length+"条】</span>";
				 var $ui = "<ul>";
				 $.each(item,function(i,k){
					 $ui = $ui + "<li>已被<span class='menu'>【"+k.menuName+"】</span>菜单,绑定在<span class='model'>【"+k.modelName+"】</span>流程下</li>";
				 });
				 $ui = $ui+"</ul>";
				 editStr = editStr + $ui+ "</a>";
				 aObj.after(editStr);
			 }
		 });
	 }
	 
	/**
	 * @description 菜单的点击事件 .<br>
	 * @date 2018/3/19 .<br>
	 * @author zcg .<br>
	 */
	function menuOnClick(event, treeId, treeNode){
		 selectMenu = treeNode.id;
		 initOrgTree();
		 updateOrgCheck(treeNode.id);
	}
	
	/**
	 * @description 初始化加载JS .<br>
	 * @date 2018/3/19 .<br>
	 * @author zcg .<br>
	 */
	$(function(){
		//初始化菜单树
		menuTreeObj=$.fn.zTree.init($("#menuTree"), menuTreeSetting, menusNodes);
		var nodes = menuTreeObj.getSelectedNodes();
		if (nodes.length>0) {
			menuTreeObj.expandNode(nodes[0], true, true, true);
		}
		initOrgTree();
	});
	 
	/**
	 * @description 初始化组织树 .<br>
	 * @date 2018/3/19 .<br>
	 * @author zcg .<br>
	 */ 
	function initOrgTree(){
			//初始化组织树
			orgTreeObj=$.fn.zTree.init($("#orgTree"), orgTreeSetting, orgs);
			// 不选择父节点
			//orgTreeObj.setting.check.chkboxType = { "Y": "", "N": "" };
			
	 }
	 
	 /**
	  * @description 隐藏已被其他流程选中的节点 .<br>
	  * @date 2018/3/19 .<br>
	  * @author zcg .<br>
	  */
	 function hiddenCheckBox(data){
		 var nodes = orgTreeObj.transformToArray(orgTreeObj.getNodes());
		 $.each(nodes,function(index,item){
			var flag = checkIsHandUpOrg(data,item.id);
			//废弃 var flag = checkIsHandUp(item.id);
			if(flag){
				if(item.parentTId){
					item.nocheck = true;
					orgTreeObj.setChkDisabled(item, true);
				}else{
					var checkId = item.tId+'_check';
					$("#"+checkId).remove();
				}
			}
		 });
	 }
	 
	 /**
	  * @description 检查改菜单是否已被其他流程挂接 .<br>
	  * @date 2018/3/19 .<br>
	  * @author zcg .<br>
	  */
	 function checkIsHandUpOrg(data,orgId){
		 var modelKey = '${param.modelKey}';
		 var flag = false;
		 $.each(data,function(index,item){
			 //当单据id和组织id已绑定时，不允许绑定
			 if(orgId==item.orgId&&modelKey!=item.modelKey&&item.menuId==selectMenu){
				 flag = true; 
			 }
		 });
		 return flag;
	 }
	 
	 /**
	  * @description 得到没有被配置的组织 .<br>
	  * @date 2018/3/19 .<br>
	  * @author zcg .<br>
	  */
	 function getNewOrgs(){
		 var array = new Array();
		 $.each(orgs,function(index,item){
			 var flag = checkIsSet(item.id);
			 if(!flag){
				 array.push(item);
			 }
		 });
		 return array;
	 }
	 
	 /**
	  * @description 检测该组织是否已配置流程 .<br>
	  * @date 2018/3/19 .<br>
	  * @author zcg .<br>
	  */
	 function checkIsSet(orgId){
		 var flag = false;
		 $.each(selects,function(index,item){
			 if(item.menuId==selectMenu&&item.orgId==orgId){
				 flag = true;
				 return flag;
			 }
		 });
		 return flag;
	 }
	  
	 /**
	  * @description 检测该组织是否已挂接流程 .<br>
	  * @date 2018/3/19 .<br>
	  * @author zcg .<br>
	  */
	 function checkIsHandUp(orgId){
		 var flag = false;
		 $.each(selects,function(index,item){
			 if(item.orgId==orgId){
				 flag = true;
				 return flag;
			 }
		 });
		 return flag;
	 }
	
	/**
	 * @description 提交前事件,检测是否配置了节点和菜单 .<br>
	 * @date 2018/3/19 .<br>
	 * @author zcg .<br>
	 */
	function beforeSubmit(){
		//获取选中的菜单
		var nodes = menuTreeObj.getSelectedNodes();
		if(nodes.length>0){
			$('#menuId').val(nodes[0].id);
		}
		//获取勾选的组织,得到所有的组织Id集合
		var nodes = orgTreeObj.getCheckedNodes(true);
		var selectids=[];
		for(var i=0; i<nodes.length; i++) {
			selectids[i]=nodes[i].id;
		}
		$("#orgIds").val(selectids);
		return true;
	}
	 
	/**
	 * @description 更新组织的选择 .<br>
	 * @date 2018/3/19 .<br>
	 * @author zcg .<br>
	 */
	 function updateOrgCheck(menuId){
		 var orgIds = new Array();
		 var modelKey = '${param.modelKey}';
		 var url = '${adminPath}//workflow/actmenuorg/findSelectedOrgs';
		 $.ajax({
			   type: 'POST',
			   url: url ,
			   data: {modelKey:modelKey,menuId:menuId} ,
			   dataType: 'json',
			   success: function(data){
				   //如果单据被本单据使用，则不允许隐藏
				   hiddenCheckBox(data);
				   $(data).each(function(index,item){
					   var node = orgTreeObj.getNodeByParam("id", item.orgId);
					   try{orgTreeObj.checkNode(node, true, false);}catch(e){}
				   })
				   orgTreeObj.expandAll(true);
			   } 
		});
		return orgs;
	 }
	
	</script>
</body>
</html>